package myky.com.doctorapp.presenter;

import com.xiangyao.train.utils.RxScedulerHelper;

import java.util.concurrent.TimeUnit;

import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import myky.com.doctorapp.base.BasePresenterIm;
import myky.com.doctorapp.base.RxSubscribe;
import myky.com.doctorapp.base.SimpleSubscribe;
import myky.com.doctorapp.contract.ChangePassWordContract;
import myky.com.doctorapp.entity.LogoutEntity;
import myky.com.doctorapp.entity.PreRequestEntuty;
import myky.com.doctorapp.model.ChangePassWordModel;

public class ChangePassWordPresenter extends BasePresenterIm<ChangePassWordContract.View> implements ChangePassWordContract.Presenter {
    ChangePassWordModel changePassWordModel;

    public ChangePassWordPresenter() {
        changePassWordModel = new ChangePassWordModel();
    }


    @Override
    public void upPassWord(String oldPwd, String newPwd, String confirmPwd) {




        changePassWordModel.upPassWord()
                .compose(RxScedulerHelper.INSTANCE.io_main())
                .doOnNext(preRequestEntity -> {


                    if (preRequestEntity.getType() != 303) {
                        mView.setButtonEnable();
                    }
                })
                .filter(preRequestEntity -> preRequestEntity.getType() == 303)
                .observeOn(Schedulers.io())
                .flatMap((Function<PreRequestEntuty, ObservableSource<PreRequestEntuty>>)
                        realChangePwdObservable ->
                                changePassWordModel.upPassWord(oldPwd, newPwd, confirmPwd))
                .delay(500, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleSubscribe<PreRequestEntuty>() {
                    @Override
                    protected void onSuccess(PreRequestEntuty preRequestEntuty) {
                        mView.setButtonEnable();
                        mView.hideLoading();


                        if (preRequestEntuty.getType() == 3) {

                            mView.changePwdSuccesed();

                        } else {

                            mView.changePwdFailed("修改密码失败");

                        }
                    }

                    @Override
                    protected void onFailed(String msg) {
                        mView.setButtonEnable();
                        mView.hideLoading();

                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                        addSubscription(d);
                        mView.showLoadingView("");
                    }
                });

    }

    @Override
    public void plogout() {

        changePassWordModel.logout().subscribe(new RxSubscribe<LogoutEntity.PayLoadBean>() {
            @Override
            protected void onSuccess(LogoutEntity.PayLoadBean payLoadBean) {
                mView.logout();
            }

            @Override
            protected void onFailed(int code, String msg) {

            }

            @Override
            public void onSubscribe(Disposable d) {

                addSubscription(d);

            }

        });

    }
}
